package com.ruoyi.project.system.device.controller;

public class UnionFloatArrayManual {

    public static void main(String[] args) {
        float floatValue = 2091390.13f;
        Float.intBitsToFloat(Integer.parseInt("49FF4BF1",16)) ;
        // 将 float 转换为字节数组
        byte[] byteArray = floatToByteArray(floatValue);

        // 打印原始 float 值
        System.out.println("Original float value: " + floatValue);

        // 打印字节数组的十六进制表示
        System.out.print("Byte array in hexadecimal: ");
        for (byte b : byteArray) {
            System.out.printf("%02X ", b);
        }
        System.out.println();
    }

    // 将 float 转换为字节数组
    public static byte[] floatToByteArray(float value) {
        int intBits = Float.floatToIntBits(value);
        return new byte[] {
                (byte) ((intBits >> 24) & 0xFF),
                (byte) ((intBits >> 16) & 0xFF),
                (byte) ((intBits >> 8) & 0xFF),
                (byte) (intBits & 0xFF)
        };
    }

    // 将字节数组转换为 float
    public static float byteArrayToFloat(byte[] byteArray) {
        int intBits = (byteArray[0] << 24) |
                ((byteArray[1] & 0xFF) << 16) |
                ((byteArray[2] & 0xFF) << 8) |
                (byteArray[3] & 0xFF);
        return Float.intBitsToFloat(intBits);
    }
}
